﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Web;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Configuration;
using AutoEncodeDaemon.Properties;

namespace AutoEncodeDaemon
{
    public partial class Config : Form
    {
        public Config()
        {
            InitializeComponent();
        }

        private void GetCookie_Click(object sender, EventArgs e)
        {
            string CookieText="";
            CookieContainer cookieJar = new CookieContainer(); 
            HttpWebRequest Request = WebRequest.Create("https://chdbits.org/takelogin.php") as HttpWebRequest;
            Request.Method = "POST";
            Request.Referer = "https://chdbits.org/login.php";
            Request.CookieContainer = cookieJar;
            string postData = "username="+Username.Text+"&password="+Password.Text;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            Request.ContentType = "application/x-www-form-urlencoded";
            Request.ContentLength = byteArray.Length;
            Stream dataStream = Request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            HttpWebResponse Response = Request.GetResponse() as HttpWebResponse;
            foreach (Cookie c in cookieJar.GetCookies(new Uri("https://chdbits.org")))
            {
                CookieText += c.Name + "=" + c.Value + ";";
            }
            if (String.IsNullOrEmpty(CookieText) == true) MessageBox.Show("输入的用户名或密码有误");
            else Cookie.Text = CookieText;
        }
        private string GetPath(string path)
        {
            if (Path.IsPathRooted(path) == true) return path;
            else return Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), path);
        }

        private void Config_Load(object sender, EventArgs e)
        {
            RssDownPath.Text = GetPath(Settings.Default.TorrentJobPath);
            UTDownPath.Text = GetPath(Settings.Default.ReleaseJobPath);
            UTTorrentPath.Text = GetPath(Settings.Default.TorrentPath);
            EncodeTempPath.Text = GetPath(Settings.Default.TempPath);
            x264Pass1.Text = Settings.Default.x264pass1;
            x264Pass2.Text = Settings.Default.x264pass2;
            CheckFileSize.Checked = Settings.Default.CheckFileSize;
            MaxBitrate.Text = Settings.Default.MaxBitrate.ToString();
            MinBitrate.Text = Settings.Default.MinBitrate.ToString();
            FallbackBitrate.Text = Settings.Default.FallbackBitrate.ToString();
            JavaPath.Text = GetPath(Settings.Default.JavaPath);
            ProxyServerAddress.Text = Settings.Default.ProxyServer;
            ProxyServerPort.Text = Settings.Default.ProxyPort.ToString();
            Cookie.Text = Settings.Default.Cookie;
        }

        private void Config_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.Default.TorrentJobPath = RssDownPath.Text;
            Settings.Default.ReleaseJobPath = UTDownPath.Text;
            Settings.Default.TorrentPath = UTTorrentPath.Text;
            Settings.Default.TempPath = EncodeTempPath.Text;
            Settings.Default.x264pass1 = x264Pass1.Text;
            Settings.Default.x264pass2 = x264Pass2.Text;
            Settings.Default.CheckFileSize = CheckFileSize.Checked;
            Settings.Default.MaxBitrate = Convert.ToInt32(MaxBitrate.Text);
            Settings.Default.MinBitrate = Convert.ToInt32(MinBitrate.Text);
            Settings.Default.FallbackBitrate = Convert.ToInt32(FallbackBitrate.Text);
            Settings.Default.JavaPath = JavaPath.Text;
            Settings.Default.ProxyServer = ProxyServerAddress.Text;
            Settings.Default.ProxyPort = Convert.ToInt32(ProxyServerPort.Text);
            Settings.Default.Cookie = Cookie.Text;

            Settings.Default.DGDecode = GetPath(Settings.Default.DGDecode);
            Settings.Default.ColorMatrix = GetPath(Settings.Default.ColorMatrix);
            Settings.Default.yadif = GetPath(Settings.Default.yadif);
            Settings.Default.ProjectX = GetPath(Settings.Default.ProjectX);
            Settings.Default.ProjectXConfig = GetPath(Settings.Default.ProjectXConfig);

            Settings.Default.Save();
        }

        private void OpenDatabase_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "AutoEncodeDaemon.exe";
            p.StartInfo.Arguments = "/viewdb";
            p.Start();
        }
    }
}
